package com.wanmait.schedule.controller;


import com.wanmait.schedule.entity.Data;
import com.wanmait.schedule.service.DataService;
import com.wanmait.schedule.service.TaskService;
import com.wanmait.schedule.util.Result;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.UUID;

/**
 * <p>
 * 材料表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/data")
public class DataController {
    @Resource
    private DataService dataService;
    @PostMapping("member/memberButton")
    public Result memberButton(Data data, MultipartFile file){
        //材料上传判断
        if(file != null && !file.isEmpty()){
            if(file.getSize()>1024*1024*100){
                return Result.error("文件上传大小不能超过100M");
            }
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            /*if (!",jpg,jpeg,gif,webp,png,pdf".contains(","+ext.toLowerCase()+",")){
                return Result.error("文件格式有问题");
            }*/
            try {
                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadFiles";
                String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
                file.transferTo(new File(path,fileName));
                String fileSuffix = "."+ext;
                data.setType(fileSuffix);
                data.setLocation(path);
                data.setName(fileName);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("文件上传出错");
            }
        }
        dataService.save(data);
        return Result.success("任务提交成功");
    }
    @PostMapping("member/memberCompanyButton")
    public Result memberCompanyButton(Data data, MultipartFile file){
        //材料上传判断
        if(file != null && !file.isEmpty()){
            if(file.getSize()>1024*1024*100){
                return Result.error("文件上传大小不能超过100M");
            }
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            /*if (!",jpg,jpeg,gif,webp,png,pdf".contains(","+ext.toLowerCase()+",")){
                return Result.error("文件格式有问题");
            }*/
            try {
                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadFiles";
                String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
                file.transferTo(new File(path,fileName));
                String fileSuffix = "."+ext;
                data.setType(fileSuffix);
                data.setLocation(path);
                data.setName(fileName);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("文件上传出错");
            }
        }
        dataService.save(data);
        return Result.success("任务提交成功");
    }
}
